import request from '@/request/request'

// 获取积分设置
export function getPointsSettings() {
  return request({
    url: '/api/admin/points/settings',
    method: 'get'
  })
}

// 更新积分设置 - 后端需要的是键值对列表格式
export function updatePointsSettings(formData) {
  // 转换前端表单对象为后端需要的积分设置列表格式
  const settingsList = [
    {
      settingKey: 'dailyCheckInPoints',
      settingValue: String(formData.dailyCheckInPoints),
      settingDescription: '每日签到获得的积分'
    },
    {
      settingKey: 'enableContinuousCheckIn',
      settingValue: formData.enableContinuousCheckIn ? '1' : '0',
      settingDescription: '是否启用连续签到额外奖励'
    },
    {
      settingKey: 'sevenDaysPoints',
      settingValue: String(formData.sevenDaysPoints),
      settingDescription: '连续签到7天获得的额外积分'
    },
    {
      settingKey: 'thirtyDaysPoints',
      settingValue: String(formData.thirtyDaysPoints),
      settingDescription: '连续签到30天获得的额外积分'
    },
    {
      settingKey: 'inviteFriendPoints',
      settingValue: String(formData.inviteFriendPoints),
      settingDescription: '邀请好友注册获得的积分'
    },
    {
      settingKey: 'friendPurchasePercent',
      settingValue: String(formData.friendPurchasePercent),
      settingDescription: '好友购买商品后获得的积分比例'
    },
    {
      settingKey: 'watchCoursePoints',
      settingValue: String(formData.watchCoursePoints),
      settingDescription: '观看课程视频获得的积分'
    },
    {
      settingKey: 'courseTestPoints',
      settingValue: String(formData.courseTestPoints),
      settingDescription: '完成课程测试获得的积分'
    },
    {
      settingKey: 'reviewPoints',
      settingValue: String(formData.reviewPoints),
      settingDescription: '评价商品/课程获得的积分'
    }
  ]

  return request({
    url: '/api/admin/points/settings',
    method: 'post',
    data: settingsList
  })
}

// 获取用户积分列表
export function getUserPointsList(pageNum = 1, pageSize = 10, userId = null) {
  const params = { pageNum, pageSize }
  if (userId) {
    params.userId = userId
  }
  
  return request({
    url: '/api/admin/points/list',
    method: 'get',
    params
  })
}

// 手动调整用户积分
export function adjustUserPoints(userId, adjustment) {
  return request({
    url: '/api/admin/points/adjust',
    method: 'post',
    params: {
      userId,
      adjustment
    }
  })
} 